﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Kaiser.Infrastructure.Controllers;
using Kaiser.Modules.Hiner.Models.Supplier.SearchModels;
using Kaiser.Models;
using Kaiser.Modules.Hiner.Services;
using Kaiser.Modules.Hiner.Models.Supplier;
using Kaiser.Models.JsonModels;
using Kaiser.Extensions;
using System.Web.Script.Serialization;

namespace Kaiser.Modules.Hiner.Controllers
{
    public class SupplierManageController : KaiserBaseController
    {
        #region 变量和属性
        private readonly ISupplierService _supplierManageService;

        public SupplierManageController(ISupplierService supplierManageService)
        {
            this._supplierManageService = supplierManageService;
        }

        #endregion

        public ActionResult SupplierList()
        {
            return View();
        }

        /// <summary>
        /// 供应商列表
        /// </summary>
        /// <param name="search"></param>
        /// <param name="pageinfo"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SupplierList(SupplierSearch search, PagingInfo pageinfo, FormCollection form)
        {
           
            DataGrid<Models.Supplier.SupplierOrder> result = _supplierManageService.SearchSupplier(search, pageinfo);
   
            return Content(result.ToJsonString());
        }

        public ActionResult GetSupplierList(SupplierSearch search) 
        {
            var pageinfo = new PagingInfo();
            pageinfo.Size = 10;
            search.SupplierCode = Request["q"];
            search.SupplierName = Request["q"];
            var list = _supplierManageService.SearchSupplier(search, pageinfo);
        
            return Json(list.rows, "text/html");
            
        }

        /// <summary>
        /// 新增供应商
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult AddSupplier(SupplierBase model)
        {
            return EditSupplier("", model);
        }

        /// <summary>
        /// 编辑供应商
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult EditSupplier(string id, SupplierBase model)
        {
            var result = new JsonReturnMessages();

            try
            {

                if (id.IsNullOrWhiteSpace())
                {
                    model.SupplierId = Guid.NewGuid().GenerateGuid().ToString();
                    model.CreateDateTime = DateTime.Now;
                    model.CreateUserFullName = User.Identity.Name;
                    model.CreateUserName = CurrentUser.Name;
                    _supplierManageService.Insert(model);
                }
                else
                {
                    SupplierBase oldSort = _supplierManageService.Get(id);
                    model.CreateDateTime = oldSort.CreateDateTime;
                    model.CreateUserFullName = oldSort.CreateUserFullName;
                    model.CreateUserName = oldSort.CreateUserName;
                    model.LastModifyDateTime = DateTime.Now;
                    model.LastModifyUserFullName = User.Identity.Name;
                    model.LastModifyUserName = CurrentUser.Name;
                    _supplierManageService.Update(model);
                }
                result.success = true;
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return Json(result, "text/html");
        }

        /// <summary>
        /// 批量删除供应商
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelSupplier(string ids)
        {
            var result = new JsonReturnMessages(); ;
            if (!ids.IsNullOrWhiteSpace())
            {
                try
                {
                    _supplierManageService.Delete(ids);
                    result.success = true;
                    result.msg = "删除成功";
                }
                catch (Exception)
                {
                    result.success = false;
                    result.msg = "删除失败";
                   
                }
            }
            else
            {
                result.msg = "传值失败，请告诉开发者解决";
            }
            var json = new JavaScriptSerializer();
            return Content(json.Serialize(result), "text/html;charset=UTF-8");
        }

        public ActionResult ViewSupplier(string id) 
        {
            SupplierBase oldSort = _supplierManageService.Get(id);
            
            return View(oldSort);
        }
    }
}
